package controllers

import play.api._
import play.api.mvc._
import play.api.data._

import models._
import views._

/**
 * Provide security features
 */
trait Secured extends Security.AllAuthenticated {
  /**
   * Retrieve the connected user email.
   */
  override def username(request: RequestHeader) = request.session.get("email")

  /**
   * Redirect to login if the use in not authorized.
   */
  override def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.Application.login)

  /**
   * Check if the connected user is a member of this project.
   */
  def IsMemberOf(project: Long)(f: => Result)(implicit request: RequestHeader) = {
    request.username.filter(_ => true).map(_ => f).getOrElse(Results.Forbidden)
  }

  /**
   * Check if the connected user is a owner of this task.
   */
  def IsOwnerOf(task: Long)(f: => Result)(implicit request: RequestHeader) = {
    request.username.filter(_ => true).map(_ => f).getOrElse(Results.Forbidden)
  }
}
